/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode reverseList(ListNode head) {
        ListNode p1;
        ListNode p2 = null;
        while(head != null) {
            p1=head.next;
            head.next=p2;
            p2=head;
            head=p1;
        }
        return p2;
    }
}